package com.sys.dao.impl;

import java.sql.SQLException;
import java.util.Collections;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.sys.dao.FileDao;
import com.sys.domain.File;
import com.sys.service.UploadOperationSer;
import com.sys.service.impl.UploadOperationImpl;
import com.sys.utils.C3P0Utils;

public class FileDaoImpl implements FileDao {
	private QueryRunner queryRunner;
	public FileDaoImpl() {
		queryRunner = new QueryRunner(C3P0Utils.getDataSource());
	}
	
	@Override
	public Boolean insertRecord(File file) throws SQLException {
		String sql = "insert into files values(?,?,?,?,?,?)";
		int row = queryRunner.update(sql, file.getFileId(), file.getUpUser(), file.getFileType(), file.getStatus(), file.getSize(), file.getUpTime());
		return row>0?true:false;
	}

	@Override
	public Boolean changeStatus(String fileId, String status) throws SQLException {
		String sql = "update files set status = ? where file_id = ?";
		int row = queryRunner.update(sql, status, fileId);
		return row>0?true:false;
	}
	@Override
	public Boolean isFileExist(String fileId) throws SQLException {
		String sql = "select * from files where file_id = ?";
		File file = queryRunner.query(sql, new BeanHandler<File>(File.class),fileId);
		return file!=null?true:false;
	}
	@Override
	public Boolean deleteFile(String fileId) throws SQLException {
		String sql = "delete from files where file_id = ?";
		int row = queryRunner.update(sql, fileId);
		return row>0?true:false;
	}
	@Override
	public List<File> findAllFileThatCanBeDownload() throws SQLException {
		String sql = "select file_id as fileId, up_user as upUser, file_type as fileType, size as size, up_time as upTime from files where status = 1";
		List<File> files = queryRunner.query(sql, new BeanListHandler<File>(File.class));
		UploadOperationSer uOS = new UploadOperationImpl();
		for(File eFile: files) {
			eFile.setFileId(uOS.deleteUserIdOfFile(eFile.getFileId()));
		}
		if(files.size() > 0)
			Collections.sort(files);
		return files;
	}
	
	@Override
	public List<File> findFileThatUserSearch(String fileId) throws SQLException {
		String sql = "select file_id as fileId, up_user as upUser, file_type as fileType, size as size, up_time as upTime from files where file_id like ?";
		List<File> files = queryRunner.query(sql, new BeanListHandler<File>(File.class), '%'+fileId+'%');
		UploadOperationSer uOS = new UploadOperationImpl();
		for(File eFile: files) {
			eFile.setFileId(uOS.deleteUserIdOfFile(eFile.getFileId()));
		}
		if(files.size() > 0)
			Collections.sort(files);
		return files;
	}

	@Override
	public List<File> findFileThatUserUpload(String upId) throws SQLException {
		String sql = "select file_id as fileId, up_user as upUser, file_type as fileType, size as size, up_time as upTime from files where up_user = ?";
		List<File> files = queryRunner.query(sql, new BeanListHandler<File>(File.class), upId);
		UploadOperationSer uOS = new UploadOperationImpl();
		for(File eFile: files) {
			eFile.setFileId(uOS.deleteUserIdOfFile(eFile.getFileId()));
		}
		if(files.size() > 0)
			Collections.sort(files);
		return files;
	}
	
	@Override
	public List<File> allFiles() throws SQLException {
		String sql = "select status as status, file_id as fileId, up_user as upUser, file_type as fileType, size as size, up_time as upTime from files";
		List<File> files = queryRunner.query(sql, new BeanListHandler<File>(File.class));
		UploadOperationSer uOS = new UploadOperationImpl();
		for(File eFile: files) {
			eFile.setFileId(uOS.deleteUserIdOfFile(eFile.getFileId()));
		}
		if(files.size() > 0)
			Collections.sort(files);
		return files;
	}

	@Override
	public Boolean changeFileType(String fileId, String fileType) throws SQLException {
		String sql = "update files set file_type = ? where file_id = ?";
		int row = queryRunner.update(sql, fileType, fileId);
		return row>0?true:false;
	}

}
